package com.travel.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.travel.base.BaseDao;
import com.travel.base.impl.BaseServiceImpl;
import com.travel.bean.RouteOrder;
import com.travel.dao.RouteOrderDao;
import com.travel.service.RouteOrderService;
import com.travel.utils.DateAddDay;
import com.travel.utils.PageHelper;
@Service("routeOrderServiceImpl")
public class RouteOrderServiceImpl extends BaseServiceImpl<RouteOrder> implements RouteOrderService {
	
	@Resource(name="routeOrderDaoImpl")
	private RouteOrderDao routeOrderDao;
	@Override
	public BaseDao<RouteOrder> getBaseDao() {
		// TODO Auto-generated method stub
		return routeOrderDao;
	}
	/**
	 * 添加一个多人包的订单
	 * days为票过期时间
	 */
	@Override
	public boolean addOrders(List<RouteOrder> routeOrders,int days) {
		// TODO Auto-generated method stub
		//生成订单编号的随机数
		long rand=System.currentTimeMillis();
		String random =String.valueOf(rand);
		String no=random.substring(2, 8);
		
		if(routeOrders!=null&&!routeOrders.isEmpty()){
			try {
				for (RouteOrder routeOrder : routeOrders) {
					Date endtime=DateAddDay.addAndSubtractDaysByCalendar(routeOrder.getBeginTime(), days);
					
					routeOrder.setOrderNo(no);;
					routeOrder.setEndTime(endtime);
					routeOrderDao.saveEntry(routeOrder);
				}
				return true;
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				return false;
			}
		}
		return false;
	}
	/**
	 * 按条件分页查询所有的用户订单
	 * @param map 条件集合
	 * @param pageNum 页码
	 * @param pageSize 页大小
	 * @return
	 */
	public List<RouteOrder> getOrdersByPage(Map map,Integer pageNum,Integer pageSize){
		
		return null;
	}
	/**
	 * 按条件查询分页集合
	 */
	@Override
	public PageHelper getPageByMap(Map map, Integer pageNum, Integer pageSize) {
		// TODO Auto-generated method stub
		PageHelper page=new PageHelper();
		List<RouteOrder> orders=routeOrderDao.getOrdersByPage(map, pageNum, pageSize);
		List ordrelist=new ArrayList();
		for (RouteOrder order : orders) {
			Map orderMap=new HashMap();
			orderMap.put("id", order.getId());
			orderMap.put("orderNo", order.getOrderNo());
			orderMap.put("realName", order.getRealName());
			orderMap.put("ticketName", order.getTicket().getTitle());
			orderMap.put("begintime", order.getBeginTime());
			orderMap.put("endtime", order.getEndTime());
			orderMap.put("phone", order.getPhone());
			orderMap.put("cardno",order.getCardNo());
			ordrelist.add(orderMap);
		}
		page.setList(ordrelist);
		page.setCurrentPageNo(pageNum);
		page.setPageSize(pageSize);
		page.setTotalCount(routeOrderDao.getCountByPage(map));
		return page;
	}
	
}
